<?php

session_start();

if(@$_SESSION['logged']) {

$rfid_status = fopen("rfid_status", "w");
fwrite($rfid_status, "3");
fclose($rfid_status);

parse_str($_POST['data']);

$arr = @array ('name'=>strval(trim($name)),'locker'=>intval($locker),'machine'=>intval($coffee));

$rfid_status = fopen("rfid_new_user", "w");
fwrite($rfid_status, json_encode($arr));
fclose($rfid_status);

class MyDB extends SQLite3 {
        function __construct() {
                $this->open('access.db');
        }
}

$db = new MyDB();

if(!$db) {
                echo $db->lastErrorMsg();
}

while(file_get_contents("rfid_status") != 0) {
	sleep(1);
	
}

$rfid_uid = fopen("rfid_data", "r");
$uid = fread($rfid_uid, 8);
$uid_regexp = preg_replace('/[^A-Za-z0-9 _\-\+\&]/','',$uid);
fclose($rfid_uid);

$sql = "INSERT INTO USERS(uid,login,password,name,admin) VALUES ('".$uid_regexp."','".$login."','".md5($password)."','".$name."','".$admin."')";

$results = $db->query($sql);

}


?>
